package com.song.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.song.common.enums.BusinessType;
import com.song.common.result.R;
import com.song.common.util.ExcelUtils;
import io.swagger.annotations.Api;
import com.song.framework.annotation.RateLimiter;
import com.song.module.admin.entity.ApContent;
import com.song.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.song.common.response.PageInfo;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.song.module.admin.service.IApContentService;
import com.song.module.admin.request.ApContentRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * 动态控制层
 *

 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "动态接口管理",tags = "动态接口管理")
@RequestMapping("/admin/apContent" )
public class ApContentController {

    private final IApContentService iApContentService;

    /**
     * 分页查询动态列表
     */
    @ApiOperation(value = "分页查询动态列表")
    @PostMapping("/pageList")
    public R<PageInfo<ApContent>> pageList(@RequestBody ApContentRequest apContentRequest) {
        return R.success(iApContentService.pageList(apContentRequest));
    }

    /**
     * 获取动态详细信息
     */
    @ApiOperation(value = "获取动态详细信息")
    @GetMapping(value = "/query/{id}")
    public R<ApContent> detail(@PathVariable("id") String id) {
        return R.success(iApContentService.getById(id));
    }

    /**
     * 新增动态
     */
    @Log(title = "新增动态", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增动态")
    @PostMapping("/add")
    public R<Void> add(@RequestBody ApContentRequest apContentRequest) {
        iApContentService.add(apContentRequest);
        return R.success();
    }

    /**
     * 修改动态
     */
    @Log(title = "修改动态", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改动态")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody ApContentRequest apContentRequest) {
        iApContentService.update(apContentRequest);
        return R.success();
    }

    /**
     * 删除动态
     */
    @Log(title = "删除动态", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除动态")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable String[] ids) {
        iApContentService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出动态
    *
    * @param response response
    * @throws IOException IOException
    */
    @ApiOperation(value = "导出动态")
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody ApContentRequest apContentRequest) throws IOException {
        List<ApContent> list = iApContentService.list(apContentRequest);
        ExcelUtils.exportExcel(list, ApContent.class, "动态信息", response);
    }
}
